package com.example.k8s.demo.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentDao studentDao;

    @RequestMapping("/add/{name}/{age}")
    @ResponseBody
    public Student add(@PathVariable String name, @PathVariable Integer age) {
        return studentDao.save(new Student(name, age));
    }

    @RequestMapping("/update/{id}/{name}/{age}")
    @ResponseBody
    public Student update(@PathVariable Integer id, @PathVariable String name, @PathVariable Integer age) {
        return studentDao.save(new Student(id, name, age));
    }

    @RequestMapping("/get/{id}")
    @ResponseBody
    public Student get(@PathVariable int id) {
        return studentDao.getById(id);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Student> list() {
        return studentDao.findAll();
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Integer id) {
        studentDao.deleteById(id);
        return "success";
    }

}
